package com.main.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.mysql.jdbc.Statement;

public class DbManager {
	private static Context context;
	private static Connection con;
	private static DataSource dataSource;

	private static void init() {
		try {

			context = (Context) new InitialContext().lookup("java:comp/env");
			dataSource = (DataSource) context.lookup("jdbc/TestApp");
			if (dataSource == null) {
				System.err.println("Null datasource");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static boolean proveCredentials(String uname, String password) {
		init();
		try {
			con = dataSource.getConnection();
			System.out.println("Uname=" + uname);
			System.out.println("UPasword" + password);
			java.sql.Statement st = con.createStatement();
			System.out
					.println("select UNAME, UPASSWORD from APP.USERS where UNAME='"
							+ uname + "' and UPASSWORD='" + password + "'");
			ResultSet set = st
					.executeQuery("select UNAME, UPASSWORD from APP.USERS where UNAME='"
							+ uname + "' and UPASSWORD='" + password + "'");
	
			if (set.next() == false) {
				return false;
			} else {

				return true;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}
}
